package com.caisejingyu.whales.home.devices.printer;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by guazi on 16-9-19.
 */
public class RedboxEnv {
    static final String apk = "com.jiuyan.redbox";
    volatile public BeanMachineInit init;
    String imei = null;
    Context context;
    private int printEmulateTime;
    public String version = Constants.versionName + "(" + Constants.versionCode + ")";
    private volatile String romVersion = "";
    private String networkType;
    private String arPhotos;
    private BeanReport.MachineState machineState = BeanReport.MachineState.OK;
    private BeanReport.MachineState mOldState = machineState;
    private PrintTime printTime;

    protected RedboxEnv() {

    }

    public static RedboxEnv INSTANCE() {
        return Sington.instance;
    }

    public String getPhotoCacheDir() {
        File dir = new File(Environment.getExternalStorageDirectory(), "whales/photos");
        dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public String getArPhotos() {
        File dir = new File(Environment.getExternalStorageDirectory(), "whales/ar/photos");
        dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public String getResPath() {
        File dir = new File(Environment.getExternalStorageDirectory(), "whales/ar/res");
        dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public String getPhotoAdsDir() {
        File dir = new File(Environment.getExternalStorageDirectory(), "whales/ads");
        dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public String getVideoCacheDir() {
        File dir = new File(Environment.getExternalStorageDirectory(), "whales/video");
        dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public String getScanAdDir() {
        File dir = new File(Environment.getExternalStorageDirectory(), "whales/scanad");
        dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public String getHtmlCacheDir() {
        File dir = new File(Environment.getExternalStorageDirectory(), "whales/html");
        dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public String getTestPrintCacheDir() {
        File dir = new File(Environment.getExternalStorageDirectory(), "whales/testprint");
        dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public String getLogDir() {
        File dir = new File(Environment.getExternalStorageDirectory(), "whales/log");
        dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public String getRedbodDir() {
        File dir = new File(Environment.getExternalStorageDirectory(), "whales");
        dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public String getApkCacheDir() {
        File dir = new File(Environment.getExternalStorageDirectory(), "whales/apks");
        dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public String getDebugLogDir() {
        File dir = new File(Environment.getExternalStorageDirectory(), "whales/debug");
        dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public String getConfigDir() {
        File dir = new File(Environment.getExternalStorageDirectory(), "whales/config");
        dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public String getLogPath() {
        Date d = new Date(System.currentTimeMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String s = sdf.format(d);
        File file = new File(getLogDir(), "log-" + s + ".txt");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
                return "";
            }
        }
        return file.getAbsolutePath();
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
        printTime = new PrintTime(context);
//        PackageManager manager = context.getPackageManager();
//        try {
//            PackageInfo info = manager.getPackageInfo(
//                    context.getPackageName(), 0);
//            version = info.versionName + "(" + info.versionCode + ")";
//        } catch (PackageManager.NameNotFoundException e) {
//            e.printStackTrace();
//        }
    }

    public String getAdScriptUrl() {
        return "http://10.10.108.193:9000/src/index.html";
    }

    public String getCpu() {
        String cpu = CpuId.getCpu();
        if (TextUtils.isEmpty(cpu)) {
            return "";
        } else {
            return cpu;
        }
    }

    public String getVersion() {
        return CpuId.getVersion();
    }

    public String getMac() {
        try {
            WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo info = manager.getConnectionInfo();
            String address = info.getMacAddress();
            if (TextUtils.isEmpty(address)) {
                return "";
            } else {
                return address;
            }
        } catch (Exception e) {
            return "";
        }
    }

    public String getAndroidID() {
        String androidID = Build.SERIAL;//Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        if (TextUtils.isEmpty(androidID)) {
            return "";
        } else {
            return androidID;
        }
    }

    public String getSn() {
        if (!TextUtils.isEmpty(imei))
            return imei;
        imei = CpuId.getCpuId();
        if (!TextUtils.isEmpty(imei)) {
            return imei;
        } else {
            return "";
        }
    }

    public int getPrintEmulateTime() {
        if (printEmulateTime == 0) {
            return 35;
        }
        return printEmulateTime;
    }

    public void setPrintEmulateTime(long printTime) {
        final long time = printTime / 1000;
        if (time < 120 && time > 30) {
            if (this.printEmulateTime == 0) this.printEmulateTime = (int) time;
            else this.printEmulateTime = (int) ((this.printEmulateTime + time) / 2);
        }

    }

    public int getMachineState() {
        if (machineState != null) {
            return machineState.ordinal();
        } else {
            return BeanReport.MachineState.OFFLINE.ordinal();
        }
    }

    public void setMachineState(BeanReport.MachineState state) {
        this.mOldState = this.machineState;
        this.machineState = state;
        if (mOldState != machineState) {

        }
    }

    public String getCode() {
        if (RedboxEnv.INSTANCE().init != null) {
            return RedboxEnv.INSTANCE().init.code;
        }
        return "";
    }

    public String getNetworkType() {
        return networkType;
    }

    public void setNetworkType(String networkType) {
        this.networkType = networkType;
    }


    static class Sington {
        static RedboxEnv instance = new RedboxEnv();
    }
}
